package com.shen.nettysocketchat.handler

import android.util.SparseArray
import com.shen.nettysocketchat.MessageType

/**
 * 消息处理handler工厂
 */
object MessageHandlerFactory {
    private val HANDLERS: SparseArray<IMessageHandler> = SparseArray<IMessageHandler>()

    /**
     * 根据消息类型获取对应的处理handler
     *
     * @param msgType
     * @return
     */
    fun getHandlerByMsgType(msgType: Int): IMessageHandler? {
        return if(HANDLERS.indexOfKey(msgType) >= 0)
            HANDLERS.get(msgType)
        else null
    }

    init {
        /** 单聊消息处理handler  */
        HANDLERS.put(MessageType.SINGLE_CHAT.msgType, SingleChatMessageHandler())
        /** 群聊消息处理handler  */
        HANDLERS.put(MessageType.GROUP_CHAT.msgType, GroupChatMessageHandler())
        /** 服务端返回的消息发送状态报告处理handler  */
        HANDLERS.put(MessageType.SERVER_MSG_SENT_STATUS_REPORT.msgType,
            ServerReportMessageHandler()
        )
    }
}